var TrafficStream1;
var TrafficStreamsCount = 10;
var TrafficStreams = [];
var runOnce = true;
var frameCount = 0;
function initObjects() {

    for (var i = 0; i < TrafficStreamsCount; i++) {
        var timeAndPath = getRandomPath([-1000, 1000, -1000, 1000], 20 + Math.random() * 5, 6 + Math.random() * 3);
        var color = Math.random() * 0x808008 + 0x808080;
        var TrafficStream = new OPENSTREETMAP.TrafficStream(scene, timeAndPath, null, color, null, null);
        TrafficStream.start();
        TrafficStreams.push(TrafficStream);
    }

}

function render() {

    frameCount++;
    //camera.position.x += (mouseX - camera.position.x ) * 0.1;
    //camera.position.y += (-mouseY - camera.position.y ) * 0.1;
    //camera.lookAt(scene.position);

    for (var i = 0; i < TrafficStreams.length; i++) {
        TrafficStreams[i].update();
    }

    if (runOnce) {
        runOnce = false;
    }

    renderer.render(scene, camera);
}

function getRandomPath(rect, pointCount, vectory) {
    var time = 0;
    var rectL = rect[0];
    var rectR = rect[1];
    var rectB = rect[2];
    var rectT = rect[3];
    var path = [];
    for (var i = 0; i < pointCount; i++) {
        var point = [rectL + Math.random() * (rectR - rectL), rectB + Math.random() * (rectT - rectB), 0];
        if (i > 0) {
            var randomVectory = vectory * 0.7 + Math.random() * vectory * 0.6;
            var distence = Math.sqrt((point[0] - path[i-1][1][0]) * (point[0] - path[i-1][1][0]) + (point[1] - path[i-1][1][1]) * (point[1] - path[i-1][1][1]) + (point[2] - path[i-1][1][2]) * (point[2] - path[i-1][1][2]));
            var duration = distence / randomVectory;
            time += duration;
        }
        path.push([time, point]);
    }
    return path;
}
